﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.UI.common;
using rock.DAL;
using rock.common;

namespace rock.UI.fee
{
    public partial class UserControlFeeShaoyuzhu : UserControl
    {
        DalFEE_ROOM aDalFEE_ROOM = new DalFEE_ROOM();
        DalFEE_OTHER aDalFEE_OTHER = new DalFEE_OTHER();

        private int FEE_ROOM_CURRENT_PAGE = 1;
        private int FEE_OTHER_CURRENT_PAGE = 1;
        int XiaoQuSeqid = 0;
        int DongSeqid = 0;

        public UserControlFeeShaoyuzhu()
        {
            InitializeComponent();
            //this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //this.dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            ViewUtils.setDataGridViewStlye(this.dataGridView1);
            ViewUtils.setDataGridViewStlye(this.dataGridView2);

            this.userControlLeftTree1.selectedTreeinfo = selectedTreeinfo;
 
            this.userControlCommonListPage1.pageClick = this.refreshDataRoomFee;
            this.userControlCommonListPage2.pageClick = this.refreshDataOtherFee;

            this.dateTimePicker1.Checked = false;
            this.dateTimePicker2.Checked = false;
        }
         
        private void selectedTreeinfo(TreeInfo treeinfo)
        {
            if (treeinfo.NodeType == 0)
            {
                this.XiaoQuSeqid = 0;
                this.DongSeqid = 0;
            }
            else if (treeinfo.NodeType == 1)
            {
                this.XiaoQuSeqid = treeinfo.Id;
                this.DongSeqid = 0;
            }
            else if (treeinfo.NodeType == 2)
            {
                this.XiaoQuSeqid = 0;
                this.DongSeqid = treeinfo.Id;
            }
           

            refreshDataRoomFee(1);
            refreshDataOtherFee(1);
        }

        //刷新数据
        public void refreshDataRoomFee(int goPage)
        {


            FEE_ROOM_CURRENT_PAGE = goPage;
            String search = this.textBox2.Text;

            StringBuilder where = new StringBuilder(
                 "(" +
                         
                "    " + " HOUSE_ROOM.HOUSE_NUMBER LIKE '%" + search + "%' " +
                " OR " + " RENT_CUSTOM.NAME LIKE '%" + search + "%' " +
                " OR " + " TEMP.XIAOQU_NAME LIKE '%" + search + "%' " +
                " OR " + " TEMP.DONG_NAME LIKE '%" + search + "%' " +
                ")"
                );
            if (XiaoQuSeqid > 0)
            {
                where.Append(" AND TEMP.XIAOQU_SEQID =  " + XiaoQuSeqid);
            }
            if (DongSeqid > 0)
            {
                where.Append(" AND TEMP.DONG_SEQID =  " + DongSeqid);
            }

            //this.dateTimePicker1.Checked = false;
            //this.dateTimePicker2.Checked = false;
            if (this.dateTimePicker1.Checked)
            {
                where.Append(" AND FEE_ROOM.OPERA_TIME >=  '" + this.dateTimePicker1.Value.ToString(StringUtils.YYYY_MM_DD) + "'");
            }
            if (this.dateTimePicker2.Checked)
            {
                where.Append(" AND FEE_ROOM.OPERA_TIME <= '" + this.dateTimePicker1.Value.ToString(StringUtils.YYYY_MM_DD) + "'");
            }


            //this.STATUS.DataSource = Constant.HOUSE_ROOM_STATUS_LIST;

            PageDataSet ds = aDalFEE_ROOM.GetListByPage(where.ToString(), " RECORD_TIME DESC ", FEE_ROOM_CURRENT_PAGE, Manager.getInstance().pageSize);
            this.dataGridView1.DataSource = ds.DataSet.Tables[0];


            this.userControlCommonListPage1.Inits(ds.AllRecord, this.FEE_ROOM_CURRENT_PAGE);


        }
        //添加房租费用
        private void button3_Click(object sender, EventArgs e)
        {
            FormFeeShaoRoomZhujing ffsz = new FormFeeShaoRoomZhujing();
            ffsz.oneRefreshParentData = this.refreshDataRoomFee;
            ffsz.ShowDialog(this);
        }
        //更新房租费用
        private void button8_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow!=null)
            {
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
                FormFeeShaoRoomZhujing fu = new FormFeeShaoRoomZhujing();
                fu._ModelFEE_ROOM = this.aDalFEE_ROOM.GetModel(Convert.ToInt32(seqid));
                fu.oneRefreshParentData = refreshDataRoomFee;
                fu.ShowDialog(this);

            }
            else
            {
                MessageBox.Show("请选择要更新记录.", "提示");
            }
        }
        //删除房租费用
        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow!=null)
            {
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
                int seqidInt = Convert.ToInt32(seqid);
                if (seqidInt > 0)
                {
                    DialogResult dialogResult = MessageBox.Show("请确认是否要删除 ", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dialogResult.Equals(DialogResult.OK))
                    {

                         aDalFEE_ROOM.Delete(seqidInt);
                        //刷新数据
                        this.refreshDataRoomFee(this.FEE_ROOM_CURRENT_PAGE);
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的记录.", "提示");
            }
        }
        //刷新数据
        public void refreshDataOtherFee(int goPage)
        {


            FEE_OTHER_CURRENT_PAGE = goPage;
            String search = this.textBox2.Text;

            StringBuilder where = new StringBuilder(
                 "(" +

                "       " + " HOUSE_ROOM.HOUSE_NUMBER LIKE '%" + search + "%' " +
                " OR " + " RENT_CUSTOM.NAME LIKE '%" + search + "%' " +
                " OR " + " TEMP.XIAOQU_NAME LIKE '%" + search + "%' " +
                " OR " + " TEMP.DONG_NAME LIKE '%" + search + "%' " +
                ")"
                );
            if (XiaoQuSeqid > 0)
            {
                where.Append(" AND TEMP.XIAOQU_SEQID =  " + XiaoQuSeqid);
            }
            if (DongSeqid > 0)
            {
                where.Append(" AND TEMP.DONG_SEQID =  " + DongSeqid);
            }
            if (this.dateTimePicker1.Checked)
            {
                where.Append(" AND FEE_OTHER.OPERA_TIME >=  '" + this.dateTimePicker1.Value.ToString(StringUtils.YYYY_MM_DD) + "'");
            }
            if (this.dateTimePicker2.Checked)
            {
                where.Append(" AND FEE_OTHER.OPERA_TIME <= '" + this.dateTimePicker1.Value.ToString(StringUtils.YYYY_MM_DD) + "'");
            }

            //this.STATUS.DataSource = Constant.HOUSE_ROOM_STATUS_LIST;

            PageDataSet ds = aDalFEE_OTHER.GetListByPage(where.ToString(), " RECORD_TIME DESC ", FEE_OTHER_CURRENT_PAGE, Manager.getInstance().pageSize);
            this.dataGridView2.DataSource = ds.DataSet.Tables[0];


            this.userControlCommonListPage2.Inits(ds.AllRecord, this.FEE_OTHER_CURRENT_PAGE);


        }
        //添加杂费
        private void button4_Click(object sender, EventArgs e)
        {
            FormFeeShaoOtherMoney fsm = new FormFeeShaoOtherMoney();
            fsm.oneRefreshParentData = refreshDataOtherFee;
            fsm.ShowDialog(this);
        }
        //更新杂费
        private void button5_Click(object sender, EventArgs e)
        {
            if (dataGridView2.CurrentRow!=null)
            {
                String seqid = dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells["OTHERFEE_SEQID"].Value.ToString();
                FormFeeShaoOtherMoney fu = new FormFeeShaoOtherMoney();
                fu._ModelFEE_OTHER = this.aDalFEE_OTHER.GetModel(Convert.ToInt32(seqid));
                fu.oneRefreshParentData = refreshDataOtherFee;
                fu.ShowDialog(this);

            }
            else
            {
                MessageBox.Show("请选择要更新记录.", "提示");
            }
        }
        //删除杂费
        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView2.CurrentRow!=null)
            {
                String seqid = dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells["OTHERFEE_SEQID"].Value.ToString();
                int seqidInt = Convert.ToInt32(seqid);
                if (seqidInt > 0)
                {
                    DialogResult dialogResult = MessageBox.Show("请确认是否要删除", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dialogResult.Equals(DialogResult.OK))
                    {

                        aDalFEE_OTHER.Delete(seqidInt);
                        //刷新数据
                        this.refreshDataOtherFee(this.FEE_OTHER_CURRENT_PAGE);
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的记录.", "提示");
            }
        }
        //查询
        private void button4Select_Click(object sender, EventArgs e)
        {
            refreshDataRoomFee(this.FEE_ROOM_CURRENT_PAGE);
            refreshDataOtherFee(this.FEE_OTHER_CURRENT_PAGE);
        }

        private void UserControlFeeShaoyuzhu_Load(object sender, EventArgs e)
        {
            this.refreshDataOtherFee(this.FEE_OTHER_CURRENT_PAGE);
            this.refreshDataRoomFee(this.FEE_ROOM_CURRENT_PAGE);
        }
        //双击更新租金
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.button8_Click(null, null);
        }
        //双击更新杂费
        private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            button5_Click(null, null);
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }

        private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            setStatusWithDataGrid(dataGridView1);
        }
        public void setStatusWithDataGrid(DataGridView dgv)
        {
            for (int a = 0; a < dgv.Rows.Count; a++)
            {

                try
                {

                    int status = (int)dgv.Rows[a].Cells["FEE_STATUS"].Value;

                    DataGridViewTextBoxCell dgvcc = (DataGridViewTextBoxCell)dgv.Rows[a].Cells["STATUS_STR"];
                    dgvcc.Value = Constant.getByFeeStatus(status).Name;
                }
                catch (Exception)
                {

                }
            }
        }

        private void dataGridView2_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            set2StatusWithDataGrid(dataGridView2);
        }
        public void set2StatusWithDataGrid(DataGridView dgv)
        {
            for (int a = 0; a < dgv.Rows.Count; a++)
            {

                try
                {

                    int status = (int)dgv.Rows[a].Cells["FEE_STATUS_"].Value;

                    DataGridViewTextBoxCell dgvcc = (DataGridViewTextBoxCell)dgv.Rows[a].Cells["STATUS_STR2"];
                    dgvcc.Value = Constant.getByFeeStatus(status).Name;


                    //收费方式:

                    int FEE_MODEL = (int)dgv.Rows[a].Cells["FEE_MODEL"].Value;

                    DataGridViewTextBoxCell dgvcc2 = (DataGridViewTextBoxCell)dgv.Rows[a].Cells["FEE_MODEL_STR"];
                    dgvcc2.Value = Constant.getByFeeTypeStatus(FEE_MODEL).Name;
                }
                catch (Exception)
                {

                }
            }
        }
    }
}
